/*
* $Id: ChangeQueenBeeView.cpp 121 2010-07-19 22:33:47Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ChangeQueenBeeView.h"
#include "Visual/Drawers/EditorDrawer/HeaderItem.h"
#include "Visual/Drawers/EditorDrawer/EditItem.h"
#include "Visual/Drawers/EditorDrawer/ComboBoxItem.h"
#include "Components/Journal/JournalInterface.h"
#include "Components/Journal/QueenBeeManager.h"

#define TITLE_ID			1
#define NAME_QUEEN_BEE_ID	2
#define DATE_QUEEN_BEE_ID	3
#define BREED_QUEEN_BEE_ID	4
#define FERT_QUEEN_BEE_ID	5
#define STATUS_QUEEN_BEE_ID	6
#define DESCR_QUEEN_BEE_ID	7

CChangeQueenBeeView::CChangeQueenBeeView(CState* state)
: CAddChangeView(state){}
CChangeQueenBeeView::~CChangeQueenBeeView(){}

void CChangeQueenBeeView::SetState(){
	m_pState->m_nState = JournalEnum::VIEW_CHANGE_QUEEN_BEE;
}

bool CChangeQueenBeeView::IsViewState(State state){
	return state.m_type == JournalEnum::CHANGE_QUEEN_BEE;
}

void CChangeQueenBeeView::Init(){
	CQueenBeeManager::queen_bee qb = IJournalComponent::GetJournalComponent()->GetQueenBeeManager()->GetQueenBee(m_pState->m_nIdParameter);
	m_editorDrawer.Clear();
	m_editorDrawer.AddItem(new CHeaderItem(IApplication::GetApp()->GetLanguageManager()->GetString(ADD_QUEEN_BEE_ID), TITLE_ID));
	m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(INFO_INSPECT_NAME_QB_ID),
		NAME_QUEEN_BEE_ID, editItem::TEXT, qb.s_strName));
	m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(INFO_INSPECT_DATE_QB_ID),
		DATE_QUEEN_BEE_ID, editItem::DATE, CConvertFunction::ConvertFromDate(qb.s_date)));
	CComboBoxItem* pItem = new CComboBoxItem(IApplication::GetApp()->GetLanguageManager()->GetString(INFO_INSPECT_BREED_QB_ID), BREED_QUEEN_BEE_ID);
	for(int i = 1;; i++){
		pItem->AddValueItem(i, IApplication::GetApp()->GetLanguageManager()->GetString(i + BREED_HYBRID_ID));
		if(i + BREED_HYBRID_ID == BREED_FAREAST_ID)
			break;
	}
	pItem->Sort();
	pItem->InsertValueItem(0, 0, IApplication::GetApp()->GetLanguageManager()->GetString(BREED_HYBRID_ID));
	pItem->SetValue(qb.s_nBreed);
	m_editorDrawer.AddItem(pItem);
	m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(INFO_INSPECT_STATUS_QB_ID),
		STATUS_QUEEN_BEE_ID, editItem::BOOL, (qb.s_bDead ? _T("-") : _T("+"))));
	m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(INFO_INSPECT_FERT_QB_ID),
		FERT_QUEEN_BEE_ID, editItem::TEXT, qb.s_strFert));
	m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(INFO_INSPECT_DESCR_QB_ID),
		DESCR_QUEEN_BEE_ID, editItem::TEXT, qb.s_strDescription));
}

BEGIN_MESSAGE_MAP(CChangeQueenBeeView, CAddChangeView)
	ON_COMMAND(BUTTON_SAVE_ID, OnSaveQueenBee)
END_MESSAGE_MAP()

void CChangeQueenBeeView::OnSaveQueenBee(){
	IJournalComponent::GetJournalComponent()->GetQueenBeeManager()->ChangeQueenBee(m_pState->m_nIdParameter,
		m_editorDrawer.GetValueItem(NAME_QUEEN_BEE_ID), m_editorDrawer.GetValueItem(DESCR_QUEEN_BEE_ID),
		m_editorDrawer.GetValueItem(FERT_QUEEN_BEE_ID),
		CConvertFunction::ConvertFromDate(m_editorDrawer.GetValueItem(DATE_QUEEN_BEE_ID)),
		(m_editorDrawer.GetValueItem(STATUS_QUEEN_BEE_ID) == _T("+")) ? false : true,
		_ttoi(m_editorDrawer.GetValueItem(BREED_QUEEN_BEE_ID).c_str()));
	__super::OnCancel();
}
